package freaks.terah.gui;

import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import freaks.terah.model.Data;
import freaks.terah.model.FileIO;
import freaks.terah.model.PlayerModel;

import java.awt.Color;
import java.io.IOException;

public class NewPlayerFrame extends JPanel implements ListSelectionListener, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JList<String>  goalList;
	JList<String>  shipList;
	Gui guiParent;
	JLabel errorText;
	
	int[][] startStats = {{2,3,4,1,2,0,3,3,3},{2,3,4,1,2,0,3,3,3},{2,3,4,1,2,0,3,3,3},{2,3,4,1,2,0,3,3,3}};
	int[][] useStats = {{2,3,4,1,2,0,3,3,3},{2,3,4,1,2,0,3,3,3},{2,3,4,1,2,0,3,3,3},{2,3,4,1,2,0,3,3,3}};
	private RessourceCountPanel startingRes;
	private RessourceCountPanel upkeepCost;
	int role;
	int playerNumber;
	

	public void setTempRole(int aRole) {
		role = aRole;
	}
	
	public void setTempPlayerNumber(int aPN) {
		playerNumber = aPN;
	}
	/**
	 * Create the panel.
	 */
	public NewPlayerFrame(Gui aParent) {
		guiParent = aParent;
		setPreferredSize(new Dimension(630, 263));
		setSize(new Dimension(606, 300));
		setLayout(null);
		String[] listData = {"Scientific Victory: Gain access to \"The God Computer\" technology before Terah Nova is found.",
		                     "Alien Victory: Build the \"Ascension Matrix\" before Terah Nova is found.",
		                     "Military Victory: Have the greatest military might upon reaching Terah Nova.",
		                     "Production Victory: Build at least three Cruxite Assemblers before reaching Terah Nova."};
		
		String[] shipData = {"Prison Transport","Scientific Survey Vessel", "Cargo Hauler", "Government Escape Ship"};
		
		JLabel lblChooseAGoal = new JLabel("Choose a goal");
		lblChooseAGoal.setBounds(16, 28, 128, 16);
		add(lblChooseAGoal);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(16, 47, 608, 72);
		add(scrollPane);
		goalList = new JList<String>(listData);
		scrollPane.setViewportView(goalList);
		goalList.setVisibleRowCount(4);
		
		JLabel lblChooseAMothership = new JLabel("Choose a mothership");
		lblChooseAMothership.setBounds(16, 131, 175, 16);
		add(lblChooseAMothership);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(16, 150, 159, 72);
		add(scrollPane_1);
		
		shipList = new JList<String>(shipData);
		shipList.addListSelectionListener(this);
		scrollPane_1.setViewportView(shipList);
		
		JButton btnContinue = new JButton("Continue");
		btnContinue.setBounds(316, 227, 117, 29);
		btnContinue.addActionListener(this);
		add(btnContinue);
		
		errorText = new JLabel("");
		errorText.setForeground(Color.RED);
		errorText.setBounds(16, 226, 220, 16);
		add(errorText);
		
		startingRes = new RessourceCountPanel(0, true);
		startingRes.setBounds(294, 131, 330, 32);
		add(startingRes);
		
		upkeepCost = new RessourceCountPanel(0, true);
		upkeepCost.setBounds(294, 173, 330, 32);
		add(upkeepCost);
		
		JLabel lblUpkeepCost = new JLabel("Upkeep Cost:");
		lblUpkeepCost.setBounds(187, 183, 89, 16);
		add(lblUpkeepCost);
		
		JLabel lblStartingRes = new JLabel("Starting Res:");
		lblStartingRes.setBounds(187, 139, 95, 16);
		add(lblStartingRes);

	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		int pos = shipList.getSelectedIndex();
		startingRes.setRessource(startStats[pos], null);
		upkeepCost.setRessource(useStats[pos], null);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (goalList.getSelectedIndex() == -1)
		{
            errorText.setText("You need to select a goal.");
			return;
		}
		
		if (shipList.getSelectedIndex() == -1)
		{
			errorText.setText("You need to select a mothership.");
			return;
		}
		
		PlayerModel theModel = new PlayerModel(goalList.getSelectedIndex(),shipList.getSelectedIndex(), playerNumber, role);
		
		guiParent.setTurn(1);
		
		Data[] globals = guiParent.getGlobalData(theModel);
		
		Data[] ressources = new Data[15];
		
		for (int i = 0; i < 9; i++)
			ressources[i] = new Data(startStats[shipList.getSelectedIndex()][i]);
		
		ressources[9] = new Data(0);
		ressources[10] = new Data(0);
		ressources[11] = new Data(3);
		ressources[12] = new Data(2);
		ressources[13] = new Data(3);
		ressources[14] = new Data(4);
		
		int totalData = globals.length + ressources.length;
		
		Data[] saveData = new Data[totalData];
		for (int i = 0; i < globals.length; i++)
			saveData[i] = globals[i];
		for (int i = 0; i < ressources.length; i++)
			saveData[i + globals.length] = ressources[i];
		
		FileIO io = new FileIO();
		
		String a = null;
		
		try {
			a = String.valueOf(guiParent.getID()) + "," + String.valueOf(guiParent.getTurn()) + "," +
			String.valueOf(theModel.getPlayerNumber()) + "," + io.encrypt(io.encode(saveData), 
			        guiParent.getID() + guiParent.getTurn(), theModel.getPlayerNumber());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		System.out.println(a);
		
		try {
			guiParent.setMainFrame(String.valueOf(guiParent.getID()) + "," + String.valueOf(guiParent.getTurn()) + "," +
                                   String.valueOf(theModel.getPlayerNumber()) + "," + io.encrypt(io.encode(saveData), 
                                 guiParent.getID() + guiParent.getTurn(), theModel.getPlayerNumber()),  io.encrypt("nLZEL000", 
                                         guiParent.getID() + guiParent.getTurn(), theModel.getPlayerNumber()));
		} catch (Exception e) {
			// TODO Something here
			e.printStackTrace();
			System.exit(2);
		}
	}
}
